package com.replication.jdbc;

import com.replication.log.Logger;
import com.replication.jdbc.connection.ConnectionSelector;
import com.replication.jdbc.connection.DefaultConnectionSelector;
import com.replication.jdbc.connection.MysqlConnectionSelector;
import com.replication.jdbc.connection.ReplicationConnectionProxy;

import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;

public class Driver implements java.sql.Driver {
    private static Logger logger = Logger.getLogger(Driver.class);

    static {
        try {
            DriverManager.registerDriver(new Driver());
        } catch (SQLException var1) {
            throw new RuntimeException("Can\'t register driver:" + Driver.class);
        }

        initDriver("com.mysql.jdbc.Driver");
        initDriver("oracle.jdbc.driver.OracleDriver");
        initDriver("com.ibm.db2.jdbc.app.DB2Driver");
        initDriver("com.microsoft.jdbc.sqlserver.SQLServerDriver");
        initDriver("com.sybase.jdbc.SybDriver");
        initDriver("com.informix.jdbc.IfxDriver");
        initDriver("org.postgresql.Driver");
        initDriver("net.sourceforge.jtds.jdbc.Driver");
    }

    public Driver() {
    }

    private static void initDriver(String driverName) {
        try {
            Class.forName(driverName);
            logger.info("{successfully register driver} " + driverName);
        } catch (ClassNotFoundException e) {
            ;
        }

    }

    public boolean acceptsURL(String url) throws SQLException {
        return url.startsWith(URLPrefix.URL_PREFIX);
    }

    public Connection connect(String url, Properties info) throws SQLException {
        if(!this.acceptsURL(url)) {
            throw new SQLException("Not support URL:" + url);
        } else {
            URLParser parser = new URLParser(url);
            String masterUrl = parser.getMasterUrl();
            ArrayList slavConns = new ArrayList();
            Connection mastConn = DriverManager.getConnection(masterUrl, info);
            Iterator iterator = parser.getSlaverUrl().iterator();

            while(iterator.hasNext()) {
                String selector = (String)iterator.next();
                Connection slavConn = DriverManager.getConnection(selector, info);
                slavConns.add(slavConn);
            }

            ConnectionSelector selector = this.makeConnectionSelector(masterUrl);
            logger.debug("{connection selector} " + selector.getClass().getName());
            return new ReplicationConnectionProxy(mastConn, slavConns, selector);
        }
    }

    public int getMajorVersion() {
        return 1;
    }

    public int getMinorVersion() {
        return 1;
    }

    public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
        URLParser parser = new URLParser(url);
        return DriverManager.getDriver(parser.getMasterUrl()).getPropertyInfo(url, info);
    }

    public boolean jdbcCompliant() {
        return true;
    }

    public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }

    private ConnectionSelector makeConnectionSelector(String url) {
        return (ConnectionSelector)(url.startsWith("jdbc:mysql:")?new MysqlConnectionSelector():new DefaultConnectionSelector());
    }
}
